package com.mcptt.config.ue.config.sub;

import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.ue.config.sub.sub.RelayedMcpttGroup;

public class OnNetwork {
	
	public static final String Name = "on-network";
	private static final String Name_Index = "index";
	private static final String Name_IPv6Preferred = "IPv6Preferred";
	private static final String Name_RelayService = "Relay-Service";
	
	private String index = null;
	private boolean ipv6Preferred = false;
	private boolean relayService = false;
	private RelayedMcpttGroup relayedMcpttGroup = null;
	
	public void parse(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		index = elem.getAttributeValue(Name_Index);
		List<Element> list = elem.getChildren();//��ø��ڵ���ӽڵ�  
		for(Element subElem:list) 
		{
			String name = subElem.getName();
			Log.debug(this.getClass(), "name = " + name);
			
			if(name == null
					|| name.length() <= 0)
			{
				return;
			}
			
			if(name.equals(RelayedMcpttGroup.Name))
			{
				relayedMcpttGroup = new RelayedMcpttGroup();
				relayedMcpttGroup.parse(subElem);
			}
			else if(name.equals(Name_IPv6Preferred))
			{
				String value = subElem.getText();
				ipv6Preferred = CheckData.parseBoolean(value);
			}
			else if(name.equals(Name_RelayService))
			{
				String value = subElem.getText();
				relayService = CheckData.parseBoolean(value);
			}
		}  
	}
	
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("<").append(Name).append(" ").append(Name_Index).append("=\"").append(index).append("\">").append("\r\n");
		
		strBuf.append("    ").append("<").append(Name_IPv6Preferred).append(">");
		strBuf.append(ipv6Preferred);
		strBuf.append("</").append(Name_IPv6Preferred).append(">").append("\r\n");
		
		strBuf.append("    ").append("<").append(Name_RelayService).append(">");
		strBuf.append(relayService);
		strBuf.append("</").append(Name_RelayService).append(">").append("\r\n");
		
		strBuf.append(relayedMcpttGroup);
		
		strBuf.append("</").append(Name).append(">").append("\r\n");

		return strBuf.toString();
	}
}
